package com.sangu.dianxiu.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.sangu.dianxiu.config.Constants;

/**
 * 检查工具类
 * Created by sg2 on 2016/1/13.
 */
public class CheckUtils {
    //检查app是否是第一次运行
    public static boolean isFirstRun(Context context)
    {
        SharedPreferences preferences=context.getSharedPreferences(Constants.IS_FIRST_RUN,Context.MODE_PRIVATE);
        int versionCode=preferences.getInt(Constants.VERSION_CODE_KEY, 1);
        boolean isFirstRun=preferences.getBoolean(Constants.IS_FIRST_RUN_KEY,true);
        if(isFirstRun||getVersionCode(context)!=versionCode)
        {
            preferences.edit().putInt(Constants.VERSION_CODE_KEY,getVersionCode(context)).commit();
            preferences.edit().putBoolean(Constants.IS_FIRST_RUN_KEY,false).commit();
            return true;
        }
        return false;
    }
    //获得app的版本号
    public static int getVersionCode(Context context)
    {
        int versionCode=1;
        PackageManager packageManager=context.getPackageManager();
        try {
            PackageInfo packageInfo=packageManager.getPackageInfo(context.getPackageName(),0);
            versionCode=packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }
}
